let coins = [3,7,405,436]
let amount = 8839
let result = []

function backtrack(start, path, remain){
    if(remain < 0){
        return
    }else if(remain == 0){
        result.push(path.length)
        return
    }else{
        for(let i = start; i < coins.length; i++){
            path.push(coins[i])
            remain = amount - path.reduce((sum, value) => sum + value, 0)
            backtrack(i, path, remain)
            path.pop()
        }
    }

}

backtrack(0, [], amount)

console.log(Math.min(...result))

